﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Collections;
using System.Threading;
using System.Windows.Forms;
using WFNetLib;
using System.Diagnostics;
using WFNetLib.StringFunc;

namespace 生命体征测试仪
{
    public partial class formMedical : formBase
    {
        CreativePacket XueYangPacket;
        private bool XueYang_ComListening = false;//是否没有执行完invoke相关操作              
        private bool XueYang_ComClosing = false;//是否正在关闭串口，执行Application.DoEvents， 并阻止再次invoke 
        Queue XueYangQueue;
        bool bXueYangPacketProc;
        bool bXueYangBoXing = false;
        bool bdata1 = false, bdata2 = false, bdata3 = false;
        int data1, data2, data3;
        private void tabXueYang_Enter(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            bdata1 = false;
            bdata2 = false;
            bdata3 = false;
            lXueYangFinish.Visible = false;
            XueYangTimer.Enabled = false;
            if(SysConfig.XueYangCom!=null)
            {
                SysConfig.XueYangCom.DiscardInBuffer();
                XueYangPacket = new CreativePacket();
                SysConfig.XueYangCom.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(XueYangCom_DataReceived);
                XueYangQueue = new Queue();
                bXueYangPacketProc = true;
                XueYang_ComListening = false;
                XueYang_ComClosing = false;
                bXueYangBoXing = false;
                Thread th = new Thread(new ThreadStart(XueYangPacketProc));
                th.Start();
            }
        }

        private void tabXueYang_Leave(object sender, EventArgs e)
        {
            leftTime = ExitTime;
            XueYangTimer.Enabled = false;
            if (SysConfig.XueYangCom != null)
            {
                XueYang_ComClosing = true;
                while (XueYang_ComListening)
                    Application.DoEvents();
                try
                {
                    SysConfig.XueYangCom.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(XueYangCom_DataReceived);
                }
                catch { }
                bXueYangPacketProc = false;
            }

        }
        void XueYangPacketProc()
        {
            while (bXueYangPacketProc)
            {
                if (XueYangQueue.Count != 0)
                {
                    CreativePacket XueYangPacket;
                    lock (XueYangQueue.SyncRoot)
                    {
                        XueYangPacket = (CreativePacket)XueYangQueue.Dequeue();
                    }
                    if (XueYangPacket.Header.Command == 0x51 && XueYangPacket.Header.type==0x02)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            if(BytesOP.GetBit(XueYangPacket.Data[0], 4))
                            {
                                XueYan_TanTou.Text = "探头未连接！";
                                XueYan_TanTou.Visible = true;
                                bdata1 = false;
                                bdata2 = false;
                                bdata3 = false;
                                XueYangTimer.Enabled = false;
                            }
                            else if (BytesOP.GetBit(XueYangPacket.Data[0], 3))
                            {
                                XueYan_TanTou.Text = "探头脱落！";
                                XueYan_TanTou.Visible = true;
                                bdata1 = false;
                                bdata2 = false;
                                bdata3 = false;
                                XueYangTimer.Enabled = false;
                            }
                            else if (BytesOP.GetBit(XueYangPacket.Data[0], 2))
                            {
                                XueYan_TanTou.Text = "探头故障或使用不当！";
                                XueYan_TanTou.Visible = true;
                                bdata1 = false;
                                bdata2 = false;
                                bdata3 = false;
                                XueYangTimer.Enabled = false;
                            }
                            else
                            {
                                XueYan_TanTou.Visible = false;
                            }
                        }));
                    }
                    if (XueYangPacket.Header.Command == 0x53 && XueYangPacket.Header.type == 0x01)
                    {
                        this.Invoke((EventHandler)(delegate
                        {
                            if (XueYangPacket.Data[0] == 0)
                            {
                                XueYang_spo2.Text = "--";
                            }
                            else
                            {
                                XueYang_spo2.Text = XueYangPacket.Data[0].ToString();
                                data1=XueYangPacket.Data[0];
                                bdata1 = true;
                            }
                            ushort pr = BytesOP.MakeShort(XueYangPacket.Data[2], XueYangPacket.Data[1]);
                            if (pr == 0)
                            {
                                XueYang_PR.Text = "--";
                            }
                            else
                            {
                                XueYang_PR.Text = pr.ToString();
                                data2=pr;
                                bdata2 = true;
                            }
                            if (XueYangPacket.Data[3] == 0)
                            {
                                XueYang_PI.Text = "--";
                            }
                            else
                            {
                                XueYang_PI.Text = (((float)XueYangPacket.Data[3])/10).ToString("F1");
                                data3=XueYangPacket.Data[3];
                                bdata3 = true;
                            }
                            if (BytesOP.GetBit(XueYangPacket.Data[4], 1))
                            {
                                XueYan_TanTou.Text = "探头脱落！";
                                XueYan_TanTou.Visible = true;
                                bdata1 = false;
                                bdata2 = false;
                                bdata3 = false;
                                XueYangTimer.Enabled = false;
                            }
                            else if (BytesOP.GetBit(XueYangPacket.Data[4], 3))
                            {
                                XueYan_TanTou.Text = "探头故障或使用不当！";
                                XueYan_TanTou.Visible = true;
                                bdata1 = false;
                                bdata2 = false;
                                bdata3 = false;
                                XueYangTimer.Enabled = false;
                            }
                            else
                            {
                                XueYan_TanTou.Visible = false;
                            }
                            if (bdata1 && bdata2 && bdata3)
                            {
                                XueYangTimer.Enabled = true;                                
                            }
                        }));
                    }
                    if (XueYangPacket.Header.Command == 0x52 && XueYangPacket.Header.type == 0x01)
                    {
                        Debug.WriteLine(StringsFunction.byteToHexStr(XueYangPacket.Data," "));
                        bXueYangBoXing = true;
                        this.Invoke((EventHandler)(delegate
                        {                            
                            for (int i = 0; i <(XueYangPacket.Header.Len - 2); i++)
                            {
                                byte x = XueYangPacket.Data[i];
                                if (BytesOP.GetBit(x, 7))
                                {
                                    XueYang_XinTiao.Visible = true;
                                }
                                else
                                {
                                    XueYang_XinTiao.Visible = false;
                                }
                                BytesOP.ClrBit(ref x, 7);
                                if (chart4.Series[0].Points.Count >= chart4.ChartAreas[0].AxisX.Maximum)
                                {
                                    chart4.Series[0].Points.Clear();
                                }
                                chart4.Series[0].Points.AddY(x);                                                              
                            }
                        }));
                    }
                }
            }
        }
        private void XueYangCom_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            if (XueYang_ComClosing)
                return;
            XueYang_ComListening = true;
            int rxn = SysConfig.XueYangCom.BytesToRead;
            while (rxn != 0)
            {
                rxn--;
                byte rx = (byte)SysConfig.XueYangCom.ReadByte();
                if (XueYangPacket.DataPacketed(rx))
                {
                    lock (XueYangQueue.SyncRoot)
                    {
                        XueYangQueue.Enqueue(XueYangPacket);
                    }
                    if (!bXueYangBoXing)
                    {
                        byte[] tx = CreativePacket.MakeCreativePacket(0x50, 0x02, 0x01);
                        SysConfig.XueYangCom.Write(tx, 0, tx.Length);
                    }
                    XueYangPacket = new CreativePacket();
                }
            }

            XueYang_ComListening = false;
        }
        private void XueYangTimer_Tick(object sender, EventArgs e)
        {
            XueYangTimer.Enabled = false;
            this.Invoke((EventHandler)(delegate
            {
                if (bdata1 && bdata2 && bdata3)
                {
                    XueYangTimer.Enabled = true;
                    lXueYangFinish.Visible = true;
                    XueYang_ComClosing = true;
                    while (XueYang_ComListening)
                        Application.DoEvents();
                    try
                    {
                        SysConfig.XueYangCom.DataReceived -= new System.IO.Ports.SerialDataReceivedEventHandler(XueYangCom_DataReceived);
                    }
                    catch { }
                    bXueYangPacketProc = false;
                    MedicalResult.SpO2 = data1;
                    MedicalResult.PR = data2;
                    MedicalResult.PI = data3;
                    bChange = true;
                }
            }));
        }    
    }
}
